package com.cskaoyan.service;

import com.cskaoyan.bean.order.Order;
import com.cskaoyan.bean.order.OrderData;
import com.cskaoyan.bean.order.OrderGoods;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.mapper.MarketAftersaleMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用于处理订单的service
 */
@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Override
    public OrderData queryOrders(BaseParam param, Integer[] orderStatusArray, String start, String end, Integer userIdInt, Integer orderSnInt) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<Order> orders = orderMapper.selectOrder(param.getSort(), param.getOrder(), orderStatusArray, start, end, userIdInt, orderSnInt);
        PageInfo pageInfo = new PageInfo(orders);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return OrderData.data(total,pages,param.getLimit(),param.getPage(),orders);
    }

    @Override
    public List<OrderGoods> queryOrderGoods(Integer id) {
        List<OrderGoods> orderGoods = orderMapper.selectOrderGoods(id);
        return orderGoods;
    }

    @Override
    public Order queryOrderById(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        return order;
    }

    @Override
    public Integer updateShip(Integer orderId, String shipChannel, String shipSn) {
        Integer updateShipById = orderMapper.updateShipById(orderId, shipChannel, shipSn);
        return updateShipById;
    }

    @Autowired
    MarketAftersaleMapper marketAftersaleMapper;
    @Transactional
    @Override
    public Integer updateRefund(Integer orderId, Integer refundMoney) {
        Integer updateRefundById = orderMapper.updateRefundById(orderId, refundMoney);
        //根据orderId查找售后的信息
        MarketAftersaleExample marketAftersaleExample = new MarketAftersaleExample();
        MarketAftersaleExample.Criteria criteria = marketAftersaleExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andDeletedEqualTo(false);
        //修改的售后参数
        MarketAftersale marketAftersale = new MarketAftersale();
        marketAftersale.setHandleTime(new Date());
        marketAftersale.setUpdateTime(new Date());
        marketAftersaleMapper.updateByExampleSelective(marketAftersale, marketAftersaleExample);
        return updateRefundById;
    }
    @Transactional
    @Override
    public Integer delete(Integer orderId) {
        //根据订单orderId来在marketOrderGoods中选中商品goodslist
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        //marketOrderGoods是要更新的数据,只把逻辑删除和更新时间更新
        MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
        marketOrderGoods.setUpdateTime(new Date());
        marketOrderGoods.setDeleted(true);
        marketOrderGoodsMapper.updateByExampleSelective(marketOrderGoods,marketOrderGoodsExample);
        //marketOrder的逻辑删除更新,更新时间更新
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setUpdateTime(new Date());
        marketOrder.setDeleted(true);
        int update = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return update;
    }
}
